<?php 
/**
 * 登录和注册管理控制器
 */
class PublicController extends CommonController{
	/**
	 * 登录方法
	 */
	public function login(){
		//Array
		//(
		//  [account] => admin
		//  [pwd] => admin
		//  [auto] => on
		//)
		if(IS_POST){
			$username = $_POST['account'];
			$pwd = md5($_POST['pwd']);
			$flat = NULL;
			$flat = M('hd_user')->where("username='$username' and passwd='$pwd'")->find();
//			p($flat);die;
			if($flat){
				setcookie(session_name(),session_id(),0,'./');
				if($_POST['auto']){
					setcookie(session_name(),session_id(),time()+3600*7,'./');
				}
				$_SESSION['uname'] = $username;
				$_SESSION['uid'] = $flat['uid'];
				$this->success("登录成功",__APP__);
//				$data=array(
//					'loginip' =>'',
//					'face'    =>
//				);
//				M('hd_user')->where("uid='$flat'")->update($data);
			}else{
				$f = M('hd_user')->where("username='$username'")->field('uid')->find();
				if(!$f){
					$this->error("用户名不存在！");
				}else{
					$this->error("密码错误！");
				}
			}
		}
	}
	/**
	 * 注册方法
	 */
	public function register(){
//  [username] => admin
//  [pwd] => admin
//  [pwded] => admin
//  [verify] => asdd
		if(IS_POST){
			$code = strtoupper($_POST['verify']);
			$user = htmlspecialchars($_POST['username']);
			$pwd = md5($_POST['pwd']);
			if($code != $_SESSION['code'])
				$this->error("验证码错误！");
			if($_POST['pwd'] != $_POST['pwded'])
				$this->error("两次输入密码不一致，请确认！");
			if(M('hd_user')->where("username='$user'")->find())
				$this->error("用户名已存在！");
			$data=array(
				"username" => $user,
				"passwd" => $pwd,
				"loginip"=>"",
				"face"=>"",
			);
			M('hd_user')->add($data);
			$this->success("注册成功", __APP__);
		}
	}
	public function code(){
		$code = new Code;
		$code->show();
	}
	public function out(){
		session_unset();
		session_destroy();
		$this->gourl(__APP__);
	}
}

